﻿package {
	import flash.display.Sprite;
	import flash.events.Event;
	import idv.cjcat.stardust.common.clocks.SteadyClock;
	import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
	
    [SWF(backgroundColor=0xFFFFFF, frameRate=60)]
	
    public class Main extends Sprite {
		
		public var container:Sprite = new Sprite();
		
		public var cjEmitter:CJEmitter = new CJEmitter(new SteadyClock(0.3));
		public var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(container);
		
		public function Main() {
			addChild(container);
			
			renderer.addEmitter(cjEmitter);
			
			addEventListener(Event.ENTER_FRAME, mainLoop);
		}
		
		private function mainLoop(e:Event):void {
			cjEmitter.step();
		}
    }
}

//------------------------------------------------------------------------------------------------

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Dictionary;
import flash.utils.getTimer;
import frocessing.color.ColorHSV;
import idv.cjcat.stardust.common.actions.Age;
import idv.cjcat.stardust.common.actions.DeathLife;
import idv.cjcat.stardust.common.actions.ScaleCurve;
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.initializers.Life;
import idv.cjcat.stardust.common.initializers.Scale;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.initializers.DisplayObjectClass;
import idv.cjcat.stardust.twoD.initializers.Position;
import idv.cjcat.stardust.twoD.initializers.Rotation;
import idv.cjcat.stardust.twoD.initializers.Velocity;
import idv.cjcat.stardust.twoD.zones.LazySectorZone;
import idv.cjcat.stardust.twoD.zones.SinglePoint;

//------------------------------------------------------------------------------------------------

class CJEmitter extends Emitter2D {
	
	public function CJEmitter(clock:Clock) {
		super(clock);
		
		//initializers
		addInitializer(new DisplayObjectClass(CJ));
		addInitializer(new Life(new UniformRandom(50, 0)));
		addInitializer(new Position(new SinglePoint(232.5, 232.5)));
		addInitializer(new Velocity(new LazySectorZone(4, 2)));
		addInitializer(new Scale(new UniformRandom(1, 0.3)));
		addInitializer(new Rotation(new UniformRandom(0, 30)));
		
		//actions
		addAction(new Age());
		addAction(new DeathLife());
		addAction(new Move());
		addAction(new ScaleCurve(10, 10));
	}
}

//------------------------------------------------------------------------------------------------

class Onomatopoeia extends Sprite {
	
	public static var bmpdKey:Dictionary = new Dictionary();
	
	public static function init(cls:Class, txt:String):void {
		if (bmpdKey[cls]) return;
		
		var tf:TextField = new TextField();
		var fmt:TextFormat = new TextFormat('Verdana', 64, 0xffffff, true);
        tf.defaultTextFormat = fmt;
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.text = txt;
		
		var tmp:BitmapData = new BitmapData(tf.width, tf.height, true, 0);
        tmp.draw(tf);
        var rect:Rectangle = tmp.getColorBoundsRect(0xff000000, 0xff000000, true);
        var bmpd:BitmapData = new BitmapData(rect.width, rect.height, true, 0x0);
        bmpd.draw(tmp, new Matrix(1, 0, 0, 1, -rect.x, -rect.y));
		
		bmpdKey[cls] = bmpd;
	}
	
	public function Onomatopoeia(txt:String = "Ono") {
		init(Object(this).constructor, txt);
		
		var bmp:Bitmap = addChild(new Bitmap(bmpdKey[Object(this).constructor] as BitmapData)) as Bitmap;
		bmp.x = -0.5 * bmp.width;
		bmp.y = -0.5 * bmp.height;
		
		var col:ColorHSV = new ColorHSV((getTimer() / 30) % 360);
        transform.colorTransform = new ColorTransform(col.r / 255, col.g / 255, col.b / 255);
	}
}

//------------------------------------------------------------------------------------------------

class CJ extends Onomatopoeia {
	
	public function CJ() {
		super("CJ");
	}
}